package com.blaze.framework.core.util.domain;

import com.blaze.framework.core.util.constants.Constants;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;

import static com.blaze.framework.core.util.constants.Constants.FAIL;

/**
 * 响应结果
 *
 * @author created by Zhou Zongbo on 2025/5/21.
 */
@Data
@Schema(description = "响应结果")
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @Schema(description = "响应状态码")
    private int code;
    @Schema(description = "响应消息")
    private String msg;
    @Schema(description = "响应数据")
    private T data;

    private R(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    private R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 请求成功
     *
     * @param <T> T
     * @return R
     */
    public static <T> R<T> ok() {
        return new R<>(Constants.SUCCESS, Constants.SUCCESS_MSG);
    }

    public static <T> R<T> ok(T data) {
        return new R<>(Constants.SUCCESS, Constants.SUCCESS_MSG, data);
    }

    public static <T> R<T> ok(T data, String msg) {
        return new R<>(Constants.SUCCESS, msg, data);
    }

    /**
     * 请求失败
     *
     * @param msg 错误信息
     * @param <T> T
     * @return R
     */
    public static <T> R<T> fail(String msg) {
        return new R<>(FAIL, msg);
    }

    public static <T> R<T> fail(String msg, T data) {
        return new R<>(FAIL, msg, data);
    }

    public static <T> R<T> fail(int code, String msg) {
        return new R<>(code, msg);
    }

    public static <T> R<T> fail(int code, String msg, T data) {
        return new R<>(code, msg, data);
    }
}
